﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class ImageLightbox
    {
        #region Properties
        public Image Image
        {
            get
            {
                return this.GetImage();
            }
        }

        public Status Status
        {
            get
            {
                return this.GetStatus();
            }
        }

        public Lightbox Lightbox
        {
            get
            {
                return this.GetLightbox();
            }
        }
        #endregion

        #region Methods

        public Image GetImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID == this.ID_Image);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Status GetStatus()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Status.Where(p => p.ID == this.ID_Status);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Lightbox GetLightbox()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Lightboxes.Where(p => p.ID == this.ID_Lightbox);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static ImageLightbox Parse(System.Object value)
        {
            ImageLightbox temp = null;
            try
            {
                temp = (ImageLightbox)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<ImageLightbox> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.ImageLightboxes;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion

        #region Search
        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID_Lightbox" || kvp.Key == "ID_Status" || kvp.Key == "ID_Image")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "PrimaryImage")
                {
                    if (int.Parse(kvp.Value.ToString()) == 1 || int.Parse(kvp.Value.ToString()) == 0)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
            }

            return query;
        }

        public static System.Object GetImageLightbox(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<ImageLightbox>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion
    }
}